<?php
/***********************************************************************
 * @package    	IvanEngine
 * @subpackage 	Core
 * @author     	Ivan <ivanzx@msn.com>
 * @created    	2010-05-19
 ***********************************************************************/
if(defined("IVANENGINE_CORE_VALIDATOR_DATERANGE"))
	die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_VALIDATOR_DATERANGE", true);

require_once (dirname(__FILE__).'/ValidatorMapCompare.php');

class CValidatorDateRange extends CValidatorBase
{
	const RO_FromValidator	= 'RO_FromDate';
	const RO_ToValidator	= 'RO_ToDate';
	const VP_From			= 'VP_From';
	const VP_To				= 'VP_To';
	protected function Initialize($options = array(), $messages = array())
  	{
    	$this->AddMessage(self::NM_Invalid, 'The begin date must be before the end date.');

    	$this->AddRequiredOption(self::RO_FromValidator);
    	$this->AddRequiredOption(self::RO_ToValidator);
  	}
  	
	protected function DoVerify($value)
  	{
    	$value[self::VP_From] = $this->GetOption(self::RO_FromValidator)->Verify(isset($value[self::VP_From]) ? $value[self::VP_From] : null);
	    $value[self::VP_To]   = $this->GetOption(self::RO_ToValidator)->Verify(isset($value[self::VP_To]) ? $value[self::VP_To] : null);
	
	    if ($value[self::VP_From] && $value[self::VP_To])
	    {
	      	$v = new CValidatorMapCompare(self::VP_From, CValidatorMapCompare::OP_LESS_THAN_EQUAL, self::VP_To, array(CValidatorMapCompare::NO_ThrowGlobalError => true), array(self::NM_Invalid => $this->GetMessage(self::NM_Invalid)));
	      	$v->Verify($value);
	    }
	
	    return $value;
  	}
}

?>